package com.myapp.util.spring;

import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class WebPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	
	private Properties mergedProps = null;
	
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		try {
			mergedProps = mergeProperties();

			// Convert the merged properties, if necessary.
			convertProperties(mergedProps);

			// Let the subclass process the properties.
			processProperties(beanFactory, mergedProps);
		}
		catch (IOException ex) {
			throw new BeanInitializationException("Could not load properties", ex);
		}
	}
	
	public Properties getProperties(){
		return mergedProps;
	}
	
	public String getProperty(String key) throws IOException{
		if(mergedProps == null ){
			mergedProps = mergeProperties();
		}
		if(mergedProps != null){
			return mergedProps.getProperty(key);
		}
		return null;
	}	
}
